#!/usr/bin/Rscript

#outfile = 'bot-ppt-inch-arc-inflow-mgd-overlap.txt'
#outfile = 'eka-ppt-inch-arc-inflow-mgd-overlap.txt'
#outfile = 'rsl-ppt-inch-arc-inflow-mgd-overlap.txt'
#outfile = 'eka-ppt-inch-rsl-ppt-inch-overlap.txt'
#outfile = 'eka-ppt-inch-bot-ppt-inch-overlap.txt'
#outfile = 'rsl-ppt-inch-bot-ppt-inch-overlap.txt'
#outfile = 'fdl-evp-inch-arc-inflow-inch-overlap.txt'
#outfile = 'eka-ppt-inch-arc-inflow-mgd-overlap.txt'
#outfile = 'eka-ppt-inch-rfc-1dy-ppt-inch-overlap.txt'
outfile = 'eka-ppt-inch-rfc-2dy-ppt-inch-overlap.txt'


xlab = "eka-ppt"
ylab = "rfc-1dy-ppt"

    # dep is the dependent varialb ind is the independent variable  
#ind = as.matrix(read.table('../rainfall-data/bot-ppt-inch.txt',fill=T,header=F))
#dep = as.matrix(read.table('../rainfall-data/rsl-ppt-inch.formatted.txt',fill=T,header=F))[,1:4]
dep = as.matrix(read.table('../rainfall-data/eka-ppt-inch.formatted.txt',fill=T))
#ind = as.matrix(read.table('../evap-data/fdl-pan-evap-inch.formatted.txt',fill=T,header=F))
ind = as.matrix(read.table('../rainfall-data/cnrfc-qpf-arc-inch-2dy.txt',fill=T,header=T))

indTime = ind[,1]*10000+ind[,2]*100+ind[,3]
depTime = dep[,1]*10000+dep[,2]*100+dep[,3]

overlap1 = indTime %in% depTime
overlap2 = depTime %in% indTime
indTime = indTime[overlap1]
depTime = depTime[overlap2]

ind = ind[overlap1,]
dep = dep[overlap2,]

cat('year month day',ylab,xlab,'\n',file=outfile)
write(t(cbind(dep,ind[,4])),file = outfile,ncolumns=5,append=T)

cat('Wrote',outfile,'\n')

